﻿app.controller('ChurchMemberApplicationControllers', ['$scope', 'GlobalService', 'fileUpload', 'ApplicationService', 'alertboxservice', function ($scope, GlobalService, fileUpload, ApplicationService, alertboxservice) {

    $scope.currentPage = 1;
    $scope.searchkey = "";
    $scope.sortmode = "DESC";
    $scope.sortby = "DEFAULT";
    $scope.showgridloader = true;
    $scope.viewmode = 'TABLE';
    $scope.family = {};
   
    $scope.perpage = 10;

    BindGrid();

    GlobalService.GetNameTitle().success(function (responce) { $scope.NameTitles = responce });

    $scope.ViewDetails = function (ApplicantId) {
        $scope.title = "View Applicant Details";
        $("#ViewApplicantDetails").modal({ backdrop: 'static' });
        url = "GetApplication";
        ApplicationService.Getdata(url, ApplicantId).success(function (data) {
            $scope.viewfamily = data;
        });
    }

    $scope.sort = function () {
        if ($scope.sortmode == 'ASC') {
            $scope.sortmode = 'DESC';
        }
        else {
            $scope.sortmode = 'ASC';
        }
        BindGrid();
    }

    $scope.toggle = function (modelstate, id) {
        $('#SinceWhen').val("");
        $scope.modelstate = modelstate;
        $scope.id = id;
        switch (modelstate) {
            case 'add':
                $scope.title = "Add Applicant Details";
                $scope.family = null;
                break;
            case 'edit':
                $scope.title = "Edit Applicant Details";
                url = "GetApplication";
                ApplicationService.Getdata(url, id).success(function (data) {
                    debugger;
                    $scope.family = data;
                });
                break;
        }
        $("#Family").modal({ backdrop: 'static' });
    }

    function BindGrid() {
        $scope.showgridloader = true;
        url = "GetApplications?PageNo=" + $scope.currentPage + "&SearchKey=" + $scope.searchkey + "&Sort=" + $scope.sortby + "&SortMode=" + $scope.sortmode + "&PerPage=" + $scope.perpage;
        ApplicationService.Getdata(url).success(function (data) {
            $scope.showgridloader = false;
            $scope.list = data;
            if (data.length > 0) {
                $scope.entryLimit = 10;
                $scope.filteredItems = data[0].TotalRecords;
                $scope.totalItems = data[0].TotalRecords;
            }
        });
    }

    $scope.setPage = function (pageNo) {
        $scope.currentPage = pageNo;
        BindGrid();
    };

    $scope.sort_by = function (predicate) {
        $scope.predicate = predicate;
        $scope.reverse = !$scope.reverse;
    };

    $scope.delete = function (data, dataid) {
        swal({
            title: "Are you sure?",
            text: "Your will not be able to recover " + data + " Details!",
            type: "warning",
            showCancelButton: true,
            cancelButtonClass: 'btn-outline btn-black mr-10',
            cancelButtonText: 'Cancel',
            confirmButtonClass: "btn-danger",
            confirmButtonText: "Delete",
            closeOnConfirm: false
        }, function (status) {
            if (status) {
                url = "DeleteApplication";
                ApplicationService.DeleteData(url, dataid, data).success(function (data) {
                    if (data == "DSUS") {
                        swal("Deleted!", "Details deleted successfully", "success");
                    }
                    else {
                        swal("failed", "Unable to delete this details", "error");
                    }
                    BindGrid();
                });
            }
        });
    }

    $scope.save = function (modelstate, id) {
        $scope.buttonstate = true;
        var url;
        switch (modelstate) {
            case 'add':
                url = "AddApplication";
                break;
            case 'edit':
                url = "UpdateApplication";
                break;
        }
        $scope.sortmode = "DESC";
        $scope.sortby = "DEFAULT";
        SavefamilyDetails(url);
    }

    function SavefamilyDetails(url) {
        console.log($scope.family);
        ApplicationService.Save($scope.family, url).success(function (result) {
            alertboxservice.ShowMsg(result, "Family ");
            $scope.buttonstate = false;
            BindGrid();
        });
    }

    $scope.Search = function () {
        $scope.currentPage = 1;
        BindGrid();
    }

    GlobalService.GetCountry().success(function (data) {
        $scope.countries = data;
    });

    $scope.GotoChildDedication = function (FamilyId) {
        $window.location.href = "../../Church/ChildDedication?FamilyId=" + FamilyId;
    }

    $scope.$watch('family.Family.PresentAddress.CountryId', function (newVal) {
        if (angular.isUndefined(newVal) == false) {
            GlobalService.GetStates(newVal).success(function (data) {
                $scope.states = data;
            });
        }
    });

    $scope.$watch('family.Family.PermanentAddress.CountryId', function (newVal) {
        if (angular.isUndefined(newVal) == false) {
            GlobalService.GetStates(newVal).success(function (data) {
                $scope.statesP = data;
            });
        }
    });

    $scope.$watch('family.Initial', function (newVal) {
        if (angular.isUndefined(newVal) == false) {
            GlobalService.GetStates(newVal).success(function (data) {
                $scope.states = data;
            });
        }
    });

    $scope.$watch('family.Initial', function (newVal) {
        if (angular.isUndefined(newVal) == false) {
            $scope.family.Initial = angular.uppercase(newVal);
        }
    });

    $scope.$watch('family.Family.PresentAddress.IsSameAddress', function (newVal) {
        if (angular.isUndefined(newVal) == false) {
            if (newVal == true) {
                $scope.family.Family.PermanentAddress = {};
                $scope.family.Family.PermanentAddress.CountryId = "";
                $scope.family.Family.PermanentAddress.CountryId = $scope.family.Family.PresentAddress.CountryId;
                $scope.family.Family.PermanentAddress.StateId = angular.copy($scope.family.Family.PresentAddress.StateId);
                $scope.family.Family.PermanentAddress.PermanentCity = angular.copy($scope.family.Family.PresentAddress.PresentCity);
                $scope.family.Family.PermanentAddress.PermanentAddress = angular.copy($scope.family.Family.PresentAddress.PresentAddress);
                $scope.family.Family.PermanentAddress.PermanentPincode = angular.copy($scope.family.Family.PresentAddress.PresentPinCode);
            }
            else {
                if ($scope.modelstate != 'edit') {
                    $scope.family.Family.PermanentAddress.CountryId = null;
                    $scope.family.Family.PermanentAddress.StateId = null;
                    $scope.family.Family.PermanentAddress.PermanentCity = null;
                    $scope.family.Family.PermanentAddress.PermanentAddress = null;
                    $scope.family.Family.PermanentAddress.PermanentPincode = null;
                }
            }
        }
    });

    $scope.ChangeView = function () {
        if ($scope.viewmode == 'TABLE') {
            $scope.viewmode = 'CARD';
        }
        else {
            $scope.viewmode = 'TABLE';
        }
    }

    $scope.softcopyfile = function (evt) {
        var file = evt.currentTarget.files[0];
        var reader = new FileReader();
        reader.onload = function (evt) {
            $scope.$apply(function ($scope) {
                $scope.family.ApplicantSoftCopy = evt.target.result;
            });
        };
        reader.readAsDataURL(file);
    }

    $scope.photofile = function (evt) {
        var file = evt.currentTarget.files[0];
        var reader = new FileReader();
        reader.onload = function (evt) {
            $scope.$apply(function ($scope) {
                $scope.family.ApplicantPhoto = evt.target.result;
            });
        };
        reader.readAsDataURL(file);
    }

    $scope.Status = GlobalService.GetStatus();

    $scope.Genders = GlobalService.GetGenders();

    GlobalService.GetChurchWorkerStatus().success(function (data) { $scope.ChurchWorkerStatuss = data; });

}]);